package com.hxrd.androidprint.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxrd.androidprint.DBUtils.DBUtils;
import com.hxrd.androidprint.R;
import com.hxrd.androidprint.bean.DetailBean;

import java.util.List;

/**
 * Created by Administrator on 2018/1/26.
 */

public class GoodsNameAdapter extends BaseAdapter {

    private Context context;
    private List<DetailBean> detailBeanList;

    public GoodsNameAdapter(Context context, List<DetailBean> detailBeanList){
        this.context=context;
        this.detailBeanList=detailBeanList;
    }

    @Override
    public int getCount() {
        return detailBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return detailBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;

        if (view == null) {

            view = LayoutInflater.from(context).inflate(R.layout.goods_name_dialog_item, null);
            viewHolder = new ViewHolder();
            viewHolder.tvGoodsName = (TextView) view.findViewById(R.id.tv_goods_name);
            viewHolder.isHasCodeImage = (ImageView) view.findViewById(R.id.is_has_code_image);

            //把缓存类的ViewHolder设置给View的Tag
            view.setTag(viewHolder);

        }else {

            //当view不为空的时候 可以从其中的Tag中加载相应缓存类
            viewHolder = (ViewHolder) view.getTag();
        }
        DetailBean bean=detailBeanList.get(i);
        viewHolder.tvGoodsName.setText(bean.getProduct_Name());

        if(bean.getIs_has_code().equals("1")){
            Log.e("变色的item", bean.toString());
            viewHolder.isHasCodeImage.setVisibility(View.VISIBLE);

        }else {
            viewHolder.isHasCodeImage.setVisibility(View.GONE);

        }
        String scanCount = DBUtils.queryDetailScanCount(context, bean.getWMSD_Code());
        if(bean.getPack_QTY()==Double.valueOf(scanCount)){

            viewHolder.tvGoodsName.setTextColor(Color.GRAY);
        }else {
            viewHolder.tvGoodsName.setTextColor(Color.BLACK);
        }

        return view;
    }

    //内部类处理缓存 优化ListView的使用
    class ViewHolder {
        TextView tvGoodsName;
        ImageView isHasCodeImage;

    }

}
